//
//  WelcomeController.swift
//  SinaBlog
//
//  Created by 李超 on 16/3/23.
//  Copyright © 2016年 lichao. All rights reserved.
//

import UIKit
import SDWebImage

class WelcomeController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        prepareUI()
        //先看看沙盒有无保存
        if let avatar_large = CZUserAccountTool.shareInstance.userAccount?.avatar_large {
            
           iconView.sd_setImageWithURL(NSURL(string: avatar_large), placeholderImage: UIImage(named: "avatar_default_big"))
            nameLable.text = CZUserAccountTool.shareInstance.userAccount?.screen_name
        }
        //进行下载更新
        CZUserAccountTool.shareInstance.loadUserInfo { (error) -> Void in
            
            if let avatar_large = CZUserAccountTool.shareInstance.userAccount?.avatar_large {
                
                self.iconView.sd_setImageWithURL(NSURL(string: avatar_large), placeholderImage: UIImage(named: "avatar_default_big"))
                
                self.nameLable.text = CZUserAccountTool.shareInstance.userAccount?.screen_name
            }
  
            
        }

    }
    var constraint:NSLayoutConstraint?
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        startAnimationWithIconView()
        
    }
    
    //开始动画
    func startAnimationWithIconView(){
       
        constraint?.constant = -(view.frame.height-200)
        UIView.animateWithDuration(1, delay: 0.25, usingSpringWithDamping: 0.5, initialSpringVelocity: 5, options: UIViewAnimationOptions(rawValue: 0), animations: {() -> Void in
        
            self.view.layoutIfNeeded()
        
        }, completion: {(Bool) -> Void in
            
            UIView.animateWithDuration(1, delay: 0, usingSpringWithDamping: 0.5, initialSpringVelocity: 5, options: UIViewAnimationOptions(rawValue: 0), animations: {() -> Void in
                 self.nameLable.alpha = 1
                }, completion: {(Bool) -> Void in
                print("完成动画")
                //欢迎界面动画播放完成后跳转到主界面
                AppDelegate.outSwitchViewController(true)
            
            })
           
        
        })
        
        
    }
    

    func prepareUI(){
        
        view.addSubview(welcomeImage)
        view.addSubview(iconView)
        view.addSubview(nameLable)
        
        welcomeImage.translatesAutoresizingMaskIntoConstraints = false
        iconView.translatesAutoresizingMaskIntoConstraints = false
        nameLable.translatesAutoresizingMaskIntoConstraints = false
        //welcomeImage添加约束
        view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[bg]-0-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: ["bg":welcomeImage]))
        view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[bg]-0-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: ["bg":welcomeImage]))
        //iconView添加约束
        view.addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))
        //用于做动画
         constraint = NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: -160)
        view.addConstraint(constraint!)
        view.addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 85))
        view.addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 85))
        //nameLable添加约束
        view.addConstraint(NSLayoutConstraint(item: nameLable, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))
        view.addConstraint(NSLayoutConstraint(item: nameLable, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 16))
        
        
        
    }
    
    
    //背景
    private lazy var welcomeImage:UIImageView = UIImageView(image: UIImage(named: "ad_background"))
    //图像
    private lazy var iconView:UIImageView = {
        
        let image = UIImage(named: "avatar_default_big")
        let imgView = UIImageView(image: image)
        let radiu = min((image?.size.width)!, (image?.size.height)!)*0.5
        imgView.layer.cornerRadius = radiu
        imgView.layer.masksToBounds = true
        
        return imgView
    }()
    //文字
    private lazy var nameLable:UILabel = {
    
        let label = UILabel()
        label.text = "欢迎归来"
        label.font = UIFont.systemFontOfSize(14)
        label.sizeToFit()
        label.alpha = 0
        return label
    
    }()
    
}
